package wli.hibernate;

import static org.junit.Assert.*;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import wli.hibernate.util.HibernateUtil;

public class RolesTest {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testSelectRoleAndUserList() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction transaction = null;

		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();

		Query query = session
				.createQuery("SELECT users.userId, users.userName, users.passWord from Users users");
		for (Iterator it = query.iterate(); it.hasNext();) {
			Object[] row = (Object[]) it.next();
			System.out.println("USERID: " + row[0]);
			System.out.println("USER_NAME: " + row[1]);
			System.out.println("PASSWORD: " + row[2]);
		}

		transaction.commit();
		System.out.println("==========================================");

		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();

		query = session.createQuery("select roleId, roleName from Roles");
		for (Iterator it = query.iterate(); it.hasNext();) {
			Object[] row = (Object[]) it.next();
			System.out.println("ID: " + row[0]);
			System.out.println("NAME: " + row[1]);
		}

		transaction.commit();

	}

	@Test
	public void testCreateRoles() {
		Roles usrl = new Roles();

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;

		try {
			System.out.println("Test add user roles only....");
			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();

			//usrl.setRoleId(1);
			usrl.setRoleName("admin");
			//usrl.setRoleOwner("wli");
			System.out.println("User Role [" + session.save(usrl) + "] Saved.");
			transaction.commit();
			session.close();

			System.out.println("Test add user roles only....");

			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			//usrl.setRoleId(2);
			usrl.setRoleName("manager");
			//usrl.setRoleOwner("wli");
			System.out.println("User Role [" + session.save(usrl) + "] Saved.");
			transaction.commit();
			session.close();

			System.out.println("Test add user roles only....");
			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			//usrl.setRoleId(3);
			usrl.setRoleName("manager-gui");
			//usrl.setRoleOwner("wli");

			System.out.println("User Role [" + session.save(usrl) + "] Saved.");
			transaction.commit();
			session.close();

			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			//usrl.setRoleId(4);
			usrl.setRoleName("manager-script");
			//usrl.setRoleOwner("wli");
			System.out.println("User Role [" + session.save(usrl) + "] Saved.");
			transaction.commit();
			session.close();

			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			//usrl.setRoleId(5);
			usrl.setRoleName("manager-jmx");
			//usrl.setRoleOwner("wli");
			System.out.println("User Role [" + session.save(usrl) + "] Saved.");
			transaction.commit();

			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			//usrl.setRoleId(6);
			usrl.setRoleName("manager-status");
			//usrl.setRoleOwner("wli");
			System.out.println("User Role [" + session.save(usrl) + "] Saved.");
			transaction.commit();

			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			//usrl.setRoleId(7);
			usrl.setRoleName("members");
			//usrl.setRoleOwner("wli");
			System.out.println("User Role [" + session.save(usrl) + "] Saved.");
			transaction.commit();

			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			//usrl.setRoleId(8);
			usrl.setRoleName("tomcat");
			//usrl.setRoleOwner("wli");
			System.out.println("User Role [" + session.save(usrl) + "] Saved.");
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			System.out.println(">>>>>>>>>>>>>>>>>>>>" + e);
		} finally {
			session.close();
		}
	}

}